package pool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Task implements Runnable {
		
	private static SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
	private String name;
	private Date created;
	private byte[] frame;
	
	public Task(String name, byte[] frame){
		this.name = name;
		this.frame = frame;
		this.created = new Date();
	}
	
	@Override
	public void run(){
		System.out.println(Thread.currentThread().getName() + " executing " + this);
		
		demultiplex();
	
		//System.out.println(frameStr);
		
		System.out.println("--->" + Thread.currentThread().getName() + " executed " + this);
	}
	
	public void demultiplex(){
		
		String frameStr = new String(frame);
		
		String codVehiculo = frameStr.substring(0,6);
		int codEvento = Integer.parseInt(frameStr.substring(6,8));
		Calendar fecha = Calendar.getInstance();
		fecha.set(Calendar.YEAR, Integer.parseInt(frameStr.substring(8,12)));
		fecha.set(Calendar.MONTH, Integer.parseInt(frameStr.substring(12,14)));
		fecha.set(Calendar.DATE, Integer.parseInt(frameStr.substring(14,16)));
		int segs = Integer.parseInt(frameStr.substring(16,21));
		String longitud = frameStr.substring(21,29);
		String latitud = frameStr.substring(29,38);
		int velocidad = Integer.parseInt(frameStr.substring(38,41));
		int direccion = Integer.parseInt(frameStr.substring(41,44));
		int fuente = Integer.parseInt(frameStr.substring(44,45));
		int edadDato = Integer.parseInt(frameStr.substring(45,46));
		int tempAct = Integer.parseInt(frameStr.substring(46,47));
		int tempVal = Integer.parseInt(frameStr.substring(47,50));
		int choque = Integer.parseInt(frameStr.substring(50,51));
		String puertas = frameStr.substring(51,57);
		int ignicion = Integer.parseInt(frameStr.substring(57,58)); 
		
	}
	
	@Override
	public String toString(){
		return name + ", created " + fmt.format(created);
	}		
}